package com.kai.service.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kai.service.data.response.user.RecommendUserInfoResponse;
import com.kai.service.user.entity.UserFocus;
import com.kai.service.user.mapper.UserFocusMapper;
import com.kai.service.user.service.IUserFocusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户关注表 服务实现类
 * </p>
 *
 * @author wjk
 * @since 2022-04-13
 */
@Service
public class UserFocusServiceImpl extends ServiceImpl<UserFocusMapper, UserFocus> implements IUserFocusService {

    /**
     * 关注取关
     *
     * @param fansId
     * @param bloggerId
     * @return
     */
    @Override
    public Boolean focus(Long fansId, Long bloggerId) {
        UserFocus userFocus = this.getOne(new LambdaQueryWrapper<UserFocus>().eq(UserFocus::getFansId, fansId).eq(UserFocus::getBloggerId, bloggerId));
        if (Objects.isNull(userFocus)) {
            userFocus = new UserFocus();
            userFocus.setFansId(fansId).setBloggerId(bloggerId).setCreateDatetime(new Date());
            this.save(userFocus);
        } else {
            userFocus.deleteById();
        }

        return Boolean.TRUE;
    }

    /**
     * 获取推荐用户
     *
     * @param userId
     * @return
     */
    @Override
    public List<RecommendUserInfoResponse> recommendUserInfo(Long userId) {
        List<RecommendUserInfoResponse> recommendUserInfoResponses = this.baseMapper.recommendUserId();

        return recommendUserInfoResponses.stream().peek(recommendUserInfoResponse -> {
            UserFocus userFocus = this.getOne(new LambdaQueryWrapper<UserFocus>().eq(UserFocus::getFansId, userId)
                    .eq(UserFocus::getBloggerId, recommendUserInfoResponse.getId()));
            if (Objects.nonNull(userFocus)) {
                recommendUserInfoResponse.setIsFocus(Boolean.TRUE);
            }
        }).collect(Collectors.toList());
    }
}
